package com.fc.controller;

import com.fc.anno.Log;
import com.fc.context.BaseContext;
import com.fc.dto.IncomeDTO;
import com.fc.dto.pageQuery.IncomePageQueryDTO;
import com.fc.entity.Income;
import com.fc.result.PageResult;
import com.fc.result.Result;
import com.fc.service.IIncomeService;
import com.fc.vo.DailyIncomeVO;
import com.fc.vo.LogisticsDetailVO;
import com.fc.vo.MonthlyIncomeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "收入")
@RestController
@RequestMapping("/income")
public class IncomeController {

    @Autowired
    private IIncomeService incomeService;


    /**
     * 管理员设置司机的保底工资
     *
     * @param incomeDTO
     * @return
     */
    @Log
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("管理员设置司机的保底工资")
    @PostMapping("/basicSalary")
    public Result setSalary(@RequestBody IncomeDTO incomeDTO) {
        incomeService.setSalary(incomeDTO);
        return Result.success();
    }

    /**
     * 删除收入信息
     *
     * @param id
     * @return
     */
    @Log
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("删除收入信息")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        incomeService.removeById(id);
        return Result.success();
    }

    /**
     * 修改保底收入信息
     *
     * @param incomeDTO
     * @return
     */
    @Log
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("修改保底收入信息")
    @PutMapping
    public Result update(@RequestBody IncomeDTO incomeDTO) {
        incomeService.updateSalary(incomeDTO);
        return Result.success();
    }

    /**
     * 管理员查询收入列表
     *
     * @param incomePageQueryDTO
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("管理员查询收入列表")
    @PostMapping("/page")
    public Result page(@RequestBody IncomePageQueryDTO incomePageQueryDTO) {
        PageResult pageResult = incomeService.listIncome(incomePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 司机查询自己收入列表
     *
     * @param incomePageQueryDTO
     * @return
     */
    @ApiOperation("司机查询自己收入列表")
    @PostMapping("/list")
    public Result list(@RequestBody IncomePageQueryDTO incomePageQueryDTO) {
        incomePageQueryDTO.setDriverId(BaseContext.getCurrentId());
        PageResult pageResult = incomeService.listIncome(incomePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 查询收入详细信息
     *
     * @param id
     * @return
     */
    @ApiOperation("查询收入详细信息")
    @GetMapping("/{id}")
    public Result detail(@PathVariable Long id) {
        Income income = incomeService.getById(id);
        return Result.success(income);
    }


    /**
     * 司机查询日常收入
     *
     * @return 收入信息
     */
    @ApiOperation("司机查询日收入")
    @GetMapping("/daily")
    public Result<DailyIncomeVO> daily() {
        DailyIncomeVO income = incomeService.daily();
        return Result.success(income);
    }

    /**
     * 司机查询月收入
     *
     * @return 收入信息
     */
    @ApiOperation("司机查询月收入")
    @GetMapping("/monthly")
    public Result<MonthlyIncomeVO> monthly() {
        MonthlyIncomeVO income = incomeService.monthly();
        return Result.success(income);
    }

    /**
     * 管理员查询收入汇总（包含收入与支出）
     */
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("管理员查询收入汇总（包含收入与支出）")
    @GetMapping("/detail")
    public Result<List<LogisticsDetailVO>> detail() {
        List<LogisticsDetailVO> logisticsDetailVO = incomeService.detail();
        return Result.success(logisticsDetailVO);
    }
}
